package kr.co.iniline.rmkb.core;

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

class Resource {
	static SpriteBatch sprite = null;
	static final HashMap<String, BitmapFont> fontMap = new HashMap<String,BitmapFont>();
	static final HashMap<String,TextureInfo> textureMap = new HashMap<String,TextureInfo>();
	
	public static class TextureInfo {
		private static final String DEFAULT_UV_KEY = "defaultUV";
		public static final float G_U_START = 0.0f;
		public static final float G_V_START = 0.0f;
		public static final float G_U_END = 1.0f;
		public static final float G_V_END = 1.0f;
		public static final float DEFAULT_UV[] = new float[]{0f,0f, 0f,1f, 1f,1f, 1f,0f};
		
		Texture tex;
		HashMap<String,float[]> uvMap;
		
		private TextureInfo(Texture tex, float defaultUV[]) {
			this.tex = tex;
			if( defaultUV != null ) {
				setUV(DEFAULT_UV_KEY, defaultUV);
			}
		}
		
		public Texture getTexture() {
			return tex;
		}
		
		public void setUV(String key, float uv[]) {
			if(uvMap == null) {
				uvMap = new HashMap<String,float[]>();
			}
			uvMap.put(key, uv);
		}
		
		public float[] getUV() {
			if(uvMap == null) {
				return DEFAULT_UV;
			}
			return getUV( DEFAULT_UV_KEY );
		}
		
		public float[] getUV(String key) {
			return uvMap.get(key);
		}
	}
	
	static SpriteBatch getSprite() {
		return sprite;
	}
	
	static void setFont(String key, BitmapFont font) {
		fontMap.put(key, font);
	}
	
	static BitmapFont getFont(String key) {
		return fontMap.get(key);
	}
	
	static void load() {
		sprite = new SpriteBatch();
		Resource.loadTexture("cubeface", Gdx.files.internal("image/cubeface.png"));
		Resource.loadTexture("joker", Gdx.files.internal("image/joker.png"));
		Resource.loadTexture("background", Gdx.files.internal("image/background.jpg"));
		setFont("default", new BitmapFont(false));
		setFont("h12", new BitmapFont(Gdx.files.internal("font/first.fnt"), false));
		Kub.loadKubs();
	}
	
	static void dispose() {
		Kub.disposeKubs();
		sprite.dispose();
	}
	
	static void loadTexture(String key, FileHandle fileHandle) {
		Pixmap pixMap = new Pixmap(fileHandle);
		
		/** compute 2pow */
		/** TODO : more efficient */
		int orgWidth = pixMap.getWidth();
		int orgHeight = pixMap.getHeight();
		int max = Math.max(orgWidth, orgHeight);
		int computeSize = 0x1;
		for (int i = 1; i < 10; i++) {
			computeSize = (1<<i);
			if( ( computeSize == orgWidth ) && ( computeSize == orgHeight ) ) {
				break;
			}
			if( computeSize > max ) {
				break;
			}
		}
		System.out.println( "Resource.loadTexture - Key:"+key+", width:"+orgWidth+", height:"+orgHeight+", Computed 2pow : "+computeSize);
		/** compute 2pow */
		
		Pixmap newPixmap = new Pixmap(computeSize, computeSize, Pixmap.Format.RGBA8888);
		newPixmap.drawPixmap(pixMap, 0, 0);
		float u = (float)pixMap.getWidth()/(float)max;
		float v = (float)pixMap.getHeight()/(float)max;
		float defaultUV[] = new float[]{0f,0f, 0f,v, u,v, u,0};
		
		Texture tex = new Texture(newPixmap);
		setTexture(key, new TextureInfo(tex,defaultUV));
	}
	
	static void setTexture(String key, TextureInfo texInfo) {
		textureMap.put(key, texInfo);
	}
	
	static Texture getTexture(String key) {
		return textureMap.get(key).getTexture();
	}
	
	static float[] getUV(String key) {
		return textureMap.get(key).getUV();
	}
	
	static float[] getUV(String key, String uvName) {
		return textureMap.get(key).getUV(uvName);
	}
	
	static void bindTexture(String key) {
		getTexture(key).bind();
	}
	
	static void setWrap(TextureWrap s, TextureWrap t) {
		Gdx.gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, s.getGLEnum());
		Gdx.gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, t.getGLEnum());
	}
	
	static void setFilter(TextureFilter mi, TextureFilter mx) {
		Gdx.gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, mi.getGLEnum());
		Gdx.gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, mx.getGLEnum());
	}
	
	public static void main(String[] args) {
		System.out.println( (1<<8) );
	}
}
